// pages/fill/fillform/fillform.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: -1,
    flag: true,
    edit: 0,
    certificatesBoolean: true,
    screenHeight: app.globalData.screenHeight,
    name: '',
    score: '',
    rank: '',
    phone: '',
    sexyIndex: 2,
    sexy: '男',
    sexyArr: ['男', '女'],
    addressindex: 1000,
    addressindexTwo: 1000,
    multipleContent: '优选地区(多选)',
    multipleContent1: '选择证书(多选)',
    certificates: [],
    address: [],
    certificateList: [],
    userRegionList: [],
    userInfo: '',
    showOne: false,
    showTwo: false,
    userHeight:'',
    nickname:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log('res');
    
    app.globalData.wxRequest.requestGet('certificate/getCertificateList.do', {}).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      let certificates = res.data;
      that.setData({
        certificates: certificates
      }, function () {
        app.globalData.wxRequest.customRequest('common/getAreaList.do', "GET", {}).then(res => {
          if (res.code == '201') {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
            return
          }
          that.setData({
            address: res.data
          }, function () {
            if (options.edit) {
              let openId = app.globalData.openId
              that.setData({
                edit: options.edit
              })
              // let openId = 'o0sP25CWPn1R2lhJvIaQ-bwjVfpM'
              app.globalData.wxRequest.requestGet('volunteer/getUserDetail.do', {
                openId: openId
              }).then(res1 => {
                if (res1.code == '201') {
                  wx.showToast({
                    title: res.msg,
                    icon: 'error'
                  })
                  return
                }
                let userInfo = res1.data;
                let sexyArr = that.data.sexyArr;
                let sexyIndex = that.data.sexyIndex;
                console.log(userInfo)
                sexyArr.map((item, index) => {
                  if (item == userInfo.sex) {
                    sexyIndex = index
                  }
                })
                let address = that.data.address;
                let addressindexTwo = that.data.addressindexTwo;
                // let userRegionList = that.data.userRegionList;
                address.map((item, index) => {
                  if (item.value == userInfo.censusRegister) {
                    addressindexTwo = index
                  }
                  // userInfo.userRegionList.map((it, i) => {
                  //   if (item.value == it.value) {
                  //     userRegionList.push(it)
                  //     address[index].type = 'choosed'
                  //   }
                  // })

                })
                let certificates = that.data.certificates;
                certificates.map((item, index) => {
                  userInfo.userCertificateList.map(it => {
                    if (item.id == it.certificateId) {
                      certificates[index].type = 'choosed'
                    }
                  })
                })
                let certificateList = that.data.certificateList;
                certificateList = userInfo.userCertificateList
                that.setData({
                  userInfo: userInfo,
                  name: userInfo.username,
                  score: userInfo.score,
                  rank: userInfo.ranking,
                  phone: userInfo.phone,
                  sexyIndex: sexyIndex,
                  userCertificateList: userInfo.userCertificateList,
                  certificates: certificates,
                  certificateList: certificateList,
                  address: address,
                  addressindexTwo: addressindexTwo,
                  // userRegionList: userRegionList,
                  userHeight:userInfo.userHeight,
                })
              }).catch(err => {
                console.log(err);
                wx.showToast({
                  title: '请求失败',
                  icon: 'error'
                })
              })
            }
          })
        }).catch(err => {
          wx.showToast({
            title: '请求失败',
            icon: 'error'
          })
        })

      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得input_select组件
    this.input_select = this.selectComponent("#input_select");
    //获得singer组件 单选
    this.singer = this.selectComponent("#singer");
    //获得multiple组件  多选
    this.multiple = this.selectComponent("#multiple");
    this.multiple1 = this.selectComponent("#multiple1");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 优先地区
  showMultiple: function (e) {
    this.setData({
      showOne: true
    }, function () {
      this.multiple.showMultiple();
    })
    // console.log(e, 's');
  },
  // 证书
  showMultiple1: function (e) {
    this.setData({
      showTwo: true
    }, function () {
      this.multiple1.showMultiple();
    })
    // console.log('s222', e, '');
  },
  // 多选返回数据
  returnData(e) {
    // console.log('多选返回数据', this, e);
  },
  // //优先地区取消事件
  // _multipleCancel(e) {
  //   // console.log('----2121212',e);
  //   this.setData({
  //     showOne: false,
  //     userRegionList:[]
  //   }, function () {
  //     this.multiple.newMultiple();
  //   })
  // },
  // 证书取消
  _multipleCancel1(e) {
    // console.log('2121212',e);
    this.setData({
      showTwo: false,
      certificateList:[]
    }, function () {
      this.multiple1.newMultiple();
    })
  },
  // //优先地区确认事件
  // _multipleConfirm(e) {
  //   this.setData({
  //     userRegionList: e.detail,
  //     showOne: false
  //   }, function () {
  //     this.multiple.hideMultiple();
  //   })
  // },
  // 证书确认事件
  _multipleConfirm1(e) {
    // console.log('-----', e, this);
    this.setData({
      certificateList: e.detail,
      showTwo: false
    }, function () {
      this.multiple1.hideMultiple();
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindPickerSexy: function (e) {
    // console.log(e);
    this.setData({
      sexyIndex: e.detail.value
    })
  },
  bindaddressChange: function (e) {
    // console.log(e);
    this.setData({
      addressindex: e.detail.value
    })
  },
  bindaddressTwoChange: function (e) {
    // console.log(e);
    this.setData({
      addressindexTwo: e.detail.value
    })
  },
  phoneNum(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  inputScore(e) {
    this.setData({
      score: e.detail.value
    })
  },
  inputRank(e) {
    this.setData({
      rank: e.detail.value
    })
  },
  // showcertificates (e) {
  //   this.setData({
  //     certificatesBoolean: !this.data.certificatesBoolean,
  //   })
  // },
  hidecertificates(e) {
    this.setData({
      certificatesBoolean: !this.data.certificatesBoolean,
    })
  },
  changeUserInfo(e) {
    let that = this;
    that.data.flag = false;
    console.log(app.globalData);
    let data = {};
    data.username = that.data.name;
    data.score = that.data.score;
    data.ranking = that.data.rank;
    // data.userRegionList = [{
    //   regionName: that.data.address[that.data.addressindex]
    // }];
    data.userHeight = that.data.userHeight;
    data.censusRegister = that.data.address[that.data.addressindexTwo].value;
    data.sex = that.data.sexyArr[that.data.sexyIndex];
    data.phone = that.data.phone;
    data.certificateList = []
    // data.userRegionList = []
    let certificateArr = that.data.certificateList;
    // let userRegionArr = that.data.userRegionList;
    certificateArr.map((item, index) => {
      if (item.certificateId) {
        data.certificateList[index] = {
          certificateId: item.certificateId
        };
      } else {
        data.certificateList[index] = {
          certificateId: item.id
        };
      }
    })
    // userRegionArr.map((item, index) => {
    //   data.userRegionList[index] = {
    //     regionName: item.value
    //   };
    // })
    data.id = app.globalData.user.id
    data.openId = app.globalData.openId
    if (!data.username) {
      wx.showModal({
        title: '提示',
        content: '请输入姓名',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (!data.score) {
      wx.showModal({
        title: '提示',
        content: '请填写分数',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    } else if (!regPos.test(data.score) && !regNeg.test(data.score)) {
      wx.showModal({
        title: '提示',
        content: '排名请填写数字',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return;
    }
    // if (!data.ranking) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请填写排名',
    //     showCancel: false,
    //     success(res) {
    //       if (res.confirm) {
    //         console.log('用户点击确定')
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    //   that.data.flag = true;
    //   return
    // } else if (!regPos.test(data.ranking) && !regNeg.test(data.ranking)) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '排名请填写数字',
    //     showCancel: false,
    //     success(res) {
    //       if (res.confirm) {
    //         console.log('用户点击确定')
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    //   that.data.flag = true;
    //   return;
    // }
    if (!data.sex) {
      wx.showModal({
        title: '提示',
        content: '请选择性别',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }

    if (!data.censusRegister) {
      wx.showModal({
        title: '提示',
        content: '请选择户籍地',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    if (!data.certificateList.length) {
      wx.showModal({
        title: '提示',
        content: '请选择证书',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    if (!data.openId) {
      wx.showModal({
        title: '提示',
        content: '无openId',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    // if (!data.userRegionList.length) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请选择优先地区',
    //     showCancel: false,
    //     success(res) {
    //       if (res.confirm) {
    //         console.log('用户点击确定')
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    //   that.data.flag = true;
    //   return
    // }

    console.log('data', data);
    app.globalData.wxRequest.request('user/update.do', data).then(res => {
      console.log('changeUserInfo', res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if(res.code == 500 ){
        wx.showToast({
          title: res.msg,
          icon:'error'
        })
      }
      that.data.flag = true;
      if (res.code == 200) {
        wx.showToast({
          title: res.msg,
          icon:'success'
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1500)
      }
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
      that.data.flag = true;
    })
  },
  addInfo(e) {
    let that = this;
    that.data.flag = false;
    console.log(app.globalData);
    let data = {};
    data.username = that.data.name;
    data.score = that.data.score;
    data.ranking = that.data.rank;
    // data.userRegionList = [{
    //   regionName: that.data.address[that.data.addressindex]
    // }];
    if (that.data.address[that.data.addressindexTwo]) {
      data.censusRegister = that.data.address[that.data.addressindexTwo].value;
    }
    data.sex = that.data.sexyArr[that.data.sexyIndex];
    data.phone = that.data.phone;
    data.certificateList = []
    // data.userRegionList = []
    let certificateArr = that.data.certificateList;
    // let userRegionArr = that.data.userRegionList;
    certificateArr.map((item, index) => {
      data.certificateList[index] = {
        certificateId: item.id
      };
    })
    // userRegionArr.map((item, index) => {
    //   data.userRegionList[index] = {
    //     regionName: item.value
    //   };
    // })
    data.userHeight = that.data.userHeight;
    data.openId = app.globalData.openId
    if (!data.username) {
      wx.showModal({
        title: '提示',
        content: '请输入姓名',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (!data.score) {
      wx.showModal({
        title: '提示',
        content: '请填写分数',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    } else if (!regPos.test(data.score) && !regNeg.test(data.score)) {
      wx.showModal({
        title: '提示',
        content: '分数请填写数字',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return;
    }
    // if (!data.ranking) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请填写排名',
    //     showCancel: false,
    //     success(res) {
    //       if (res.confirm) {
    //         console.log('用户点击确定')
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    //   that.data.flag = true;
    //   return
    // } else if (!regPos.test(data.ranking) && !regNeg.test(data.ranking)) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '排名请填写数字',
    //     showCancel: false,
    //     success(res) {
    //       if (res.confirm) {
    //         console.log('用户点击确定')
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    //   that.data.flag = true;
    //   return;
    // }
    if (!data.sex) {
      wx.showModal({
        title: '提示',
        content: '请选择性别',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }

    if (!data.censusRegister) {
      wx.showModal({
        title: '提示',
        content: '请选择户籍地',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    if (!data.certificateList.length) {
      wx.showModal({
        title: '提示',
        content: '请选择证书',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    if (!data.openId) {
      wx.showModal({
        title: '提示',
        content: '无openId',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      that.data.flag = true;
      return
    }
    // userHeight = that.data.userHeight;
    // if (!data.userRegionList.length) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请选择优先地区',
    //     showCancel: false,
    //     success(res) {
    //       if (res.confirm) {
    //         console.log('用户点击确定')
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    //   that.data.flag = true;
    //   return
    // }


    console.log('data', data);
    app.globalData.wxRequest.request('user/add.do', data).then(res => {
      console.log('addinfo', res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.data.flag = true;
      if (res.code == 200) {
        let user = res.data;
        user.isSignUp = 1;
        app.globalData.user = user;
        wx.navigateBack({
          delta: 1,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
      that.data.flag = true;
    })
  },
  inputHeight(e){
    this.setData({
      userHeight: e.detail.value
    })
  },
  getPhoneNumber(e){
    console.log(e);
    let that = this;
    let data = {}
    data.iv = e.detail.iv;
    data.encryptedData = e.detail.encryptedData;
    data.sessionKey = app.globalData.sessionKey;
    console.log(data);
    app.globalData.wxRequest.request('data/decodeUserInfo.do',data).then(res=>{
      console.log(res)
      that.setData({
        phone:res.data
      })
    }).catch(err=>{
      console.log(err);
    })

  },
  getUserProfile(e){
    wx.getUserProfile({
      desc:"用于完善会员资料",
      success:(res)=>{
        console.log(res);
        this.setData({
          userProfile: res.userInfo,
          name:res.userInfo.nickName
        })
      }
    })
    // wx.getUserProfile({
    //   desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    //   success: (res) => {
    //     this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true
    //     })
    //   }
    // })
  },

})